package cn.zn.modules.pz.his.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.invoice.inter.InvoiceRecordInfoInter;
import cn.zn.modules.invoice.inter.vo.InvoiceVO;
import cn.zn.modules.pz.inter.values.PzConf;
import cn.zn.modules.pz.cur.entity.TpPrint;
import cn.zn.modules.pz.cur.service.TpMainService;
import cn.zn.modules.pz.cur.service.TpPrintService;
import cn.zn.modules.pz.his.entity.vo.PzTicketVO;
import cn.zn.modules.pz.his.service.PzMainService;
import cn.zn.modules.zzsys.inter.annotation.Login;
import cn.zn.modules.zzsys.inter.annotation.LoginUser;
import cn.zn.modules.zzsys.inter.dto.LoginUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("web/pz/his/pzmain")
@Slf4j
public class PzMainMvcController {

    @Autowired
    private PzConf pzConf;
    @Autowired
    private PzMainService pzMainService;
    @Autowired
    private TpMainService tpMainService;
    @Autowired
    private TpPrintService tpPrintService;
    /**
     * 发票接口
     */
    @Autowired(required = false)
    private InvoiceRecordInfoInter invoiceRecordInfoInter;

    @RequestMapping("/pzList")
    public void pzList(ModelMap model) {
        model.put("initYear", pzConf.getInit().getYear());
    }

    @RequestMapping("/pzDetail")
    public void pzDetail(ModelMap model, Integer ap, String mainId) {
        PzTicketVO pzTicket = pzMainService.findTicket(ap, mainId);
        model.addAttribute("pzTicket", pzTicket);
        model.addAttribute("ap", ap);
        model.addAttribute("hasInvoice", invoiceRecordInfoInter != null);
        model.addAttribute("mainId", mainId);
    }

    @RequestMapping("/printTicket")
//    @Login
    public String printTicket(ModelMap model, @RequestParam(required = false) Boolean showPrint, @RequestParam(required = false) Integer ap, String mainId) {
        PzTicketVO pzTicket = ap != null ? pzMainService.findTicket(ap, mainId) : tpMainService.findTicket(mainId);
        boolean _showPrint = showPrint == null ? true : showPrint;
        if (_showPrint) {
            // 保存打印
            /*try {
                tpPrintService.save(TpPrint.builder()
                        .printUserCode(loginUserDTO == null ? "" : loginUserDTO.getWorkNo())
                        .tpMainId(mainId)
                        .build());
            } catch (Exception e) {
                log.error("记录打印报错", e);
            }*/
        }
        model.addAttribute("pzTicket", pzTicket);
        model.addAttribute("showPrint", _showPrint);
        // 不同院校的模板
        String alias = pzConf.getInit().getAlias();
        return "web/pz/his/print/" + (StrUtil.isNotEmpty(alias) ? alias + "/" : "") + pzTicket.getTemplateName();
    }

    @GetMapping("invoiceList/{ap}/{applyCode}")
    @ResponseBody
    public R invoiceList(@PathVariable Integer ap, @PathVariable String applyCode) {
        List<InvoiceVO> invoiceVOList = CollUtil.newArrayList();
        if (invoiceRecordInfoInter != null) {
            invoiceVOList = invoiceRecordInfoInter.findInvoice(applyCode);
        }
        return R.ok().put("list", invoiceVOList);
    }

}
